package com.cssw.fyzb.busi.api.user;


import com.cssw.fyzb.core.model.vo.user.UserInfoVo;
import com.cssw.fyzb.busi.service.user.UserService;
import com.cssw.fyzb.core.dao.user.DistrictDao;
import com.cssw.fyzb.core.model.dodb.District;
import com.cssw.fyzb.core.model.dodb.User;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;

/**
 * 获取用户信息
 *
 * @author YangYiQun
 */
@Function(description = "获取用户公开信息", parameters = {
        @Parameter(name = "userId", type = Type.String, description = "用户标识", required = true)
})
@Component
public class GetPublicUserInfo implements Action {

    @Autowired
    private UserService userService;

    @Autowired
    private DistrictDao districtDao;

    private static final Logger logger = LoggerFactory.getLogger(GetUserInfo.class);

    @Override
    public Response execute(Request request) throws Exception {
        Long userId = request.getLong("userId");
        try {
            User user = userService.getById(userId);
            UserInfoVo userInfo = new UserInfoVo();
            userInfo.setUserId(user.getUserId());
            userInfo.setSupUserId(user.getSupUserId());
            userInfo.setNickName(user.getNickName());
            userInfo.setProvince(user.getProvince());
            userInfo.setCity(user.getCity());
            userInfo.setDistrict(user.getDistrict());

            // 地区名称
            if(user.getDistrict()!=null) {
                District district = districtDao.getByCode(user.getDistrict());
                if(district!=null) {
                    userInfo.setDistrict(district.getDistrictName());
                }
            }
            // 城市名称
            if(user.getCity()!=null) {
                District district = districtDao.getByCode(user.getCity());
                if(district!=null) {
                    userInfo.setCity(district.getDistrictName());
                }
            }
            // 省
            if(user.getProvince()!=null) {
                District district = districtDao.getByCode(user.getProvince());
                if(district!=null) {
                    userInfo.setProvince(district.getDistrictName());
                }
            }

            userInfo.setStatus(user.getStatus());
            userInfo.setSex(user.getSex());

            if(user.getBirthday()!=null) {
                SimpleDateFormat formatterBirthday = new SimpleDateFormat("yyyy-MM-dd");
                userInfo.setBirthday(formatterBirthday.format(user.getBirthday()));
            }

            userInfo.setFaceAuth(user.getFaceAuth());
            userInfo.setUserLevel(user.getUserLevel());
            userInfo.setTeamLevel(user.getTeamLevel());
            if(user.getCreateTime()!=null) {
                SimpleDateFormat formatterTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                userInfo.setCreateTime(formatterTime.format(user.getCreateTime()));
            }
            userInfo.setSign(user.getSign());
            userInfo.setAvatar(user.getAvatar());

            return Response.success().put("data", userInfo);
        } catch (Exception e) {
            logger.error("", e);
            return Response.fail("用户公开信息获取失败");
        }
    }

}
